package client;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class GUI extends JLayeredPane {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/* GUI components */
	static JFrame chatFrame;
	JLayeredPane layer;
	JTextField namePane;
	JScrollPane friendPane;
	JButton addButton, avatarButton, messageButton, removeButton;
	
	/* Back end */
	JList friendList;
	String nameString = "Name";
	
	/* Statuses
	 * Online  (green)
	 * Away    (orange)
	 * Busy    (red)
	 * Offline (grey)
	 */	

	public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
	}
	
	private GUI() {
		//Top
		//avatarNamePanel
		JPanel avatarNamePanel = new JPanel();
		SpringLayout layoutTop = new SpringLayout();
		avatarNamePanel.setLayout(layoutTop);
		
		avatarButton = new JButton();
		avatarButton.addActionListener(new ActionListener() {
       	 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("Change avatar");
            }
        }); 
		avatarButton.setBorder(new LineBorder(Color.green, 1));
		ImageIcon icon = new ImageIcon("avatar.png");
		avatarButton.setIcon(icon);
		Dimension size = new Dimension(icon.getIconHeight(), icon.getIconWidth());
		avatarButton.setSize(size);
		
		namePane = new JTextField(nameString);
		namePane.addActionListener(new ActionListener() {
	       	 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("Change nickname");
            }
        }); 
		
		avatarNamePanel.add(avatarButton);
		avatarNamePanel.add(namePane);
		
		layoutTop.putConstraint(SpringLayout.WEST, avatarButton, 5, SpringLayout.WEST, avatarNamePanel);
		layoutTop.putConstraint(SpringLayout.NORTH, avatarButton, 5, SpringLayout.NORTH, avatarNamePanel);
		layoutTop.putConstraint(SpringLayout.SOUTH, avatarButton, 5, SpringLayout.SOUTH, avatarNamePanel);
		
		layoutTop.putConstraint(SpringLayout.WEST, namePane, 5, SpringLayout.EAST, avatarButton);
		layoutTop.putConstraint(SpringLayout.NORTH, namePane, 5, SpringLayout.NORTH, avatarNamePanel);
		layoutTop.putConstraint(SpringLayout.EAST, avatarNamePanel, 5, SpringLayout.EAST, namePane);
		layoutTop.putConstraint(SpringLayout.SOUTH, avatarNamePanel, 1, SpringLayout.SOUTH, namePane);
		
		//friendTitle
		
		JTextField friendTitle = new JTextField("Friend List");
		friendTitle.setEditable(false);
		
		//splitTop
		JPanel splitTop = new JPanel(new GridLayout(2,1,1,1));

		splitTop.add(avatarNamePanel);
		splitTop.add(friendTitle);
		
		//friendPane		
		String[] friends = {"Iain","Neil","Michael"};
		
		friendList = new JList(friends);
		MouseListener mouseListener = new MouseListener() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					int index = friendList.locationToIndex(e.getPoint());
					System.out.println("Double clicked on Item " + index);
					messagePrompt();
				}
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}
		};
		 friendList.addMouseListener(mouseListener);
		
		friendPane = new JScrollPane(friendList, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		
		//Split
		JSplitPane top = new JSplitPane(JSplitPane.VERTICAL_SPLIT, splitTop, friendPane);
		top.setDividerSize(0);
		
		//Bottom
		
		JPanel bottom = new JPanel();
		bottom.setLayout(new BorderLayout());
		
		addButton = new JButton("Add");		
		addButton.addActionListener(new ActionListener() {
        	 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("Add a friend");
                addFriend();
            }
        });   
        
		messageButton = new JButton("Message");
		messageButton.addActionListener(new ActionListener() {
        	 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("Send a message");
                messagePrompt();
            }
        });   
		
		removeButton = new JButton("Remove");        
		removeButton.addActionListener(new ActionListener() {
        	 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("Remove a friend");
//                if ()
//                removeFriend(f);
            }
        });   
		
		JPanel borderPanel = new JPanel();
		borderPanel.add(addButton);
		borderPanel.add(messageButton);
		borderPanel.add(removeButton);
		
		addButton.setPreferredSize(new Dimension(80,30));
		messageButton.setPreferredSize(new Dimension(100,30));
		removeButton.setPreferredSize(new Dimension(80,30));		
		
		bottom.add(borderPanel);
		
		//Root
		
		JPanel root = new JPanel();
		BorderLayout layoutRoot = new BorderLayout();
		root.setLayout(layoutRoot);
		
		root.add(top, BorderLayout.CENTER);
		root.add(bottom, BorderLayout.SOUTH);
		
		SpringLayout layoutLayer = new SpringLayout();
		this.setLayout(layoutLayer);
		add(root,JLayeredPane.DEFAULT_LAYER);
		
		layoutLayer.putConstraint(SpringLayout.WEST, root, 1, SpringLayout.WEST, this);
		layoutLayer.putConstraint(SpringLayout.NORTH, root, 1, SpringLayout.NORTH, this);
		layoutLayer.putConstraint(SpringLayout.EAST, root, 1, SpringLayout.EAST, this);
		layoutLayer.putConstraint(SpringLayout.SOUTH, root, 1, SpringLayout.SOUTH, this);
		
		setPreferredSize(new Dimension(300, 500));
		setVisible(true);
		layer = this;
		
	}
	
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("KC Chat");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JLayeredPane newContentPane = new GUI();
        frame.setContentPane(newContentPane);
		
/*        BufferedImage icon = null;
		try {
			icon = ImageIO.read(new File("icon-tran.png"));
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
        
		frame.setIconImage(icon);*/

        //Display the window.
        frame.pack();
        frame.setVisible(true);
        chatFrame = frame;
    }
    
    private void addFriend () {
    	
    	final JDialog dialog = new JDialog(chatFrame);
    	dialog.setTitle("Add a friend");
    	JPanel addFriendPanel = new JPanel();
    	addFriendPanel.setLayout(new BorderLayout());
    	
    	String nickname, computerName, email;
    	
    	JLabel computerNameLabel = new JLabel("Computer");
    	JTextField computerNameField = new JTextField("Computer");
    	JPanel computerNamePanel = new JPanel();
    	SpringLayout computerNameLayout = new SpringLayout();
    	computerNamePanel.setLayout(computerNameLayout);
    	
    	computerNamePanel.add(computerNameLabel);
    	computerNamePanel.add(computerNameField);
		
    	computerNameLayout.putConstraint(SpringLayout.WEST, computerNameLabel, 5, SpringLayout.WEST, computerNamePanel);
    	computerNameLayout.putConstraint(SpringLayout.NORTH, computerNameLabel, 5, SpringLayout.NORTH, computerNamePanel);
    	computerNameLayout.putConstraint(SpringLayout.SOUTH, computerNameLabel, 5, SpringLayout.SOUTH, computerNamePanel);
		
    	computerNameLayout.putConstraint(SpringLayout.WEST, computerNameField, 5, SpringLayout.EAST, computerNameLabel);
    	computerNameLayout.putConstraint(SpringLayout.NORTH, computerNameField, 5, SpringLayout.NORTH, computerNamePanel);
    	computerNameLayout.putConstraint(SpringLayout.EAST, computerNamePanel, 5, SpringLayout.EAST, computerNameField);
    	computerNameLayout.putConstraint(SpringLayout.SOUTH, computerNamePanel, 5, SpringLayout.SOUTH, computerNameField);
    	
    	JLabel nicknameLabel = new JLabel("Name");
    	JTextField nicknameField = new JTextField("Name");
    	JPanel nicknamePanel = new JPanel();
    	SpringLayout nicknameLayout = new SpringLayout();
    	nicknamePanel.setLayout(nicknameLayout);
    	
    	nicknamePanel.add(nicknameLabel);
    	nicknamePanel.add(nicknameField);
		
    	nicknameLayout.putConstraint(SpringLayout.WEST, nicknameLabel, 5, SpringLayout.WEST, nicknamePanel);
    	nicknameLayout.putConstraint(SpringLayout.NORTH, nicknameLabel, 5, SpringLayout.NORTH, nicknamePanel);
    	nicknameLayout.putConstraint(SpringLayout.SOUTH, nicknameLabel, 5, SpringLayout.SOUTH, nicknamePanel);
		
    	nicknameLayout.putConstraint(SpringLayout.WEST, nicknameField, 5, SpringLayout.EAST, computerNameLabel);
		nicknameLayout.putConstraint(SpringLayout.NORTH, nicknameField, 5, SpringLayout.NORTH, nicknamePanel);
		nicknameLayout.putConstraint(SpringLayout.EAST, nicknamePanel, 5, SpringLayout.EAST, nicknameField);
		nicknameLayout.putConstraint(SpringLayout.SOUTH, nicknamePanel, 5, SpringLayout.SOUTH, nicknameField);
    	
    	JLabel emailLabel = new JLabel("Email");
    	JTextField emailField = new JTextField("Email");
    	JPanel emailPanel = new JPanel();
    	SpringLayout emailLayout = new SpringLayout();
    	emailPanel.setLayout(emailLayout);
    	
    	emailPanel.add(emailLabel);
    	emailPanel.add(emailField);
		
    	emailLayout.putConstraint(SpringLayout.WEST, emailLabel, 5, SpringLayout.WEST, emailPanel);
    	emailLayout.putConstraint(SpringLayout.NORTH, emailLabel, 5, SpringLayout.NORTH, emailPanel);
    	emailLayout.putConstraint(SpringLayout.SOUTH, emailLabel, 5, SpringLayout.SOUTH, emailPanel);
		
    	emailLayout.putConstraint(SpringLayout.WEST, emailField, 5, SpringLayout.EAST, computerNameLabel);
    	emailLayout.putConstraint(SpringLayout.NORTH, emailField, 5, SpringLayout.NORTH, emailPanel);
    	emailLayout.putConstraint(SpringLayout.EAST, emailPanel, 5, SpringLayout.EAST, emailField);
    	emailLayout.putConstraint(SpringLayout.SOUTH, emailPanel, 5, SpringLayout.SOUTH, emailField);
    	
    	JButton addButton = new JButton("Add friend");
		addButton.addActionListener(new ActionListener() {
       	 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("Add friend");
            }
        }); 
		
    	JButton cancelButton = new JButton("Cancel");
    	cancelButton.addActionListener(new ActionListener() {
       	 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("Cancel adding a friend");
                dialog.setVisible(false);
            }
        }); 
    	JPanel top = new JPanel(new GridLayout(3,1,1,1));
    	top.add(nicknamePanel);
    	top.add(computerNamePanel);
    	top.add(emailPanel);
    	
    	JPanel buttonPanel = new JPanel();
    	buttonPanel.add(addButton);
    	buttonPanel.add(cancelButton);
    	
    	JTextArea speil = new JTextArea("To add a friend fill in at least one of the fields below and press the 'Add friend' button to continue.");

    	addFriendPanel.add(top, BorderLayout.CENTER);
    	addFriendPanel.add(buttonPanel, BorderLayout.SOUTH);
    	
		JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, speil, addFriendPanel);
		split.setDividerSize(0);
    	
//    	this.add(addFriendPanel, JLayeredPane.POPUP_LAYER);
    	dialog.getContentPane().add(split);
    	dialog.setBounds(0,0,200,300);
    	dialog.setVisible(true);
    	System.out.println("Add friend dialog visible");
    }
    
    private void messagePrompt () {
    	
    	final JDialog dialog = new JDialog(chatFrame);
    	dialog.setTitle("Conversation with ");
    	JPanel messagePromptPanel = new JPanel();
    	messagePromptPanel.setLayout(new BorderLayout());

      	JEditorPane editorPane = new JEditorPane();
      	editorPane.setText("Testing");
      	editorPane.setEditable(false);
    	JScrollPane scroller = new JScrollPane(editorPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    	scroller.setPreferredSize(new Dimension(200,200));
    	
    	JEditorPane textEntry = new JEditorPane();
    	JScrollPane scrollerEntry = new JScrollPane(textEntry, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    	JButton sendButton = new JButton();
    	sendButton.addActionListener(new ActionListener() {
          	 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("Send message");
            }
        }); 
    	
    	JPanel bottom = new JPanel(new BorderLayout());
//    	SpringLayout bottomLayout = new SpringLayout();
//    	bottom.setLayout(bottomLayout);
    	bottom.add(scrollerEntry, BorderLayout.CENTER);
    	bottom.add(sendButton, BorderLayout.EAST);
    	
//    	textEntry.setPreferredSize(new Dimension(400,200));
    	
/*    	bottomLayout.putConstraint(SpringLayout.WEST, textEntry, 5, SpringLayout.WEST, bottom);
    	bottomLayout.putConstraint(SpringLayout.NORTH, textEntry, 5, SpringLayout.NORTH, bottom);
    	bottomLayout.putConstraint(SpringLayout.SOUTH, textEntry, 5, SpringLayout.SOUTH, bottom);
		
    	bottomLayout.putConstraint(SpringLayout.WEST, sendButton, 5, SpringLayout.EAST, textEntry);
    	bottomLayout.putConstraint(SpringLayout.NORTH, sendButton, 5, SpringLayout.NORTH, bottom);
		bottomLayout.putConstraint(SpringLayout.EAST, bottom, 5, SpringLayout.EAST, sendButton);
		bottomLayout.putConstraint(SpringLayout.SOUTH, bottom, 5, SpringLayout.SOUTH, sendButton);*/
    	
/*    	messagePromptPanel.add(scroller, BorderLayout.CENTER);
    	messagePromptPanel.add(bottom,BorderLayout.SOUTH);*/

		JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, scroller, bottom);
		split.setDividerLocation(500);
		split.setDividerSize(2);

    	
//    	this.add(addFriendPanel, JLayeredPane.POPUP_LAYER);
    	dialog.getContentPane().add(split);
    	dialog.setBounds(0,0,400,600);
//		split.resetToPreferredSizes();
    	dialog.setVisible(true);
    	System.out.println("Message prompt dialog visible");
    }
    
    private void removeFriend(String f) {
    	System.out.println("Removing friend "+ f);
    }
}